package org.vectrics.recruiter.struts.admin;

import java.util.Properties;

import javax.servlet.jsp.JspException;

import org.apache.log4j.Logger;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.history.HistoryProvider;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.job.JobStatusType;
import org.vectrics.struts.Request;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;
import org.vectrics.user.Person;

public class JobActivateAction extends VectricsStrutsAction {
    private transient static Logger log = Logger.getLogger(JobActivateAction.class);
	public static String CURRENT_JOB_KEY = "current-job";
    //public static String ACTION_NAME = "save_job";
    public static String JOB_PARAM = "jobId";    
    public static String ACTIVATE_PARAM = "activate";    
    
    
    //private boolean newJob = false;
	//private RemoteUserProvider userManager = new RemoteUserProvider();
	//private JobManager jobManager = new JobManager();
   

    public JobActivateAction() {
    	super();
    }


	public String doExecute(VectricsActionData actionData)	throws JspException {
		Person person = null;				
		Request request = actionData.getRequest();
		
		try {
			person = CoreServiceLocator.getUserAuthService().getAuthorizedPerson(ThreadProperties.getDomain(), request);
		}
		catch (Throwable ve) {
			log.error("VectricsException while finding auth user", ve);
			throw new JspException("VectricsException while finding auth user", ve);
		}
		
		Properties invalidParams = new Properties();
        log.debug("execute() called, reading http parameters");
        String jobIdParam = request.getParameter(JOB_PARAM);
        Long jobId = null;
        if (jobIdParam != null)
            jobId = new Long(jobIdParam);
        Job job = RecruitServiceLocator.getJobProvider().findJob(jobId);
        String activateParam = request.getParameter(ACTIVATE_PARAM);
        boolean active = false;
        if (activateParam.toLowerCase().startsWith("t")) {
        	active = true;
        }
        
    	if (active) {
    		job.setStatus(RecruitServiceLocator.getJobProvider().findJobStatusType(JobStatusType.STATUS_POSTED, job.getDomain()), 
    				actionData.getAuthPerson(), 
    				RecruitServiceLocator.getJobProvider().findDefaultStatusReason(job.getDomain(), job.getCurrentStatus().getStatusType().getCode(), JobStatusType.STATUS_POSTED));
			RecruitServiceLocator.getJobProvider().updateJob(job);
    		HistoryProvider.getInstance().createHistory(Job.OBJECT_TYPE, 
    				jobId.toString(), "job_activate", person, "Changed to active");
    	} else {
    		job.setStatus(RecruitServiceLocator.getJobProvider().findJobStatusType(JobStatusType.STATUS_CREATED, job.getDomain()), 
    				actionData.getAuthPerson(), 
    				RecruitServiceLocator.getJobProvider().findDefaultStatusReason(job.getDomain(), job.getCurrentStatus().getStatusType().getCode(), JobStatusType.STATUS_UNPOSTED));
			RecruitServiceLocator.getJobProvider().updateJob(job);
    		HistoryProvider.getInstance().createHistory(Job.OBJECT_TYPE, 
    				jobId.toString(), "job_deactivate", person, "Changed to inactive");
    	}
        
		if (invalidParams.size() == 0) {
			return ("success");
		}
		else {
			return ("failure");
		}
    }


    
}
